package com.cdbkj.cloud.pos;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.androidyang.library.common.utils.GsonUtils;
import com.dawn.cloud.pos.drive.bean.BmpRequestSale;
import com.dawn.cloud.pos.drive.sdk.BmpSdk;
import com.orhanobut.logger.Logger;


public class BmpActivity extends Activity implements View.OnClickListener {


    //消费操作
    private static final String bmp_OP_SALE = "sale";
    //撤销操作
    private static final String bmp_OP_VOID = "void";
    //退款操作
    private static final String bmp_OP_REFUND = "refund";

    protected TextView tvBmpPosC;
    protected TextView tvBmpPosS;

    protected Button btnBmpPosSearch;
    protected Button btnBmpPosSale;
    protected Button btnBmpPosVoid;
    protected Button btnBmpPosRefund;
    protected Button btnBmpPosClose;


    //服务器端数据
    private String serverData;

    private boolean running = true;

    BmpSdk bmpSdk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_bmp);
        initView();
        socketConnect();
    }

    /**
     * 连接
     */
    public void socketConnect(){
        if(bmpSdk==null){
            bmpSdk = new BmpSdk();
            bmpSdk.open("QCOM-BTD");
            //设备被发现广播  蓝牙扫描时，扫描到任一远程蓝牙设备时，会发送此广播。给onreceive
            IntentFilter discoveryFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

            this.registerReceiver(bmpSdk, discoveryFilter);
            // 设备发现完成  蓝牙扫描过程结束
            IntentFilter foundFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            this.registerReceiver(bmpSdk, foundFilter);
            bmpSdk.discovery();
        }else {
            bmpSdk.getmBluetoothAdapter().startDiscovery();
        }
    }

    private void initView() {
        btnBmpPosSale = findViewById(R.id.btn_bmp_sale);
        btnBmpPosVoid = findViewById(R.id.btn_bmp_void);
        btnBmpPosRefund = findViewById(R.id.btn_bmp_refund);
        btnBmpPosSearch = findViewById(R.id.btn_bmp_search);
        btnBmpPosClose = findViewById(R.id.btn_bmp_close);

        btnBmpPosSale.setOnClickListener(this);
        btnBmpPosVoid.setOnClickListener(this);
        btnBmpPosRefund.setOnClickListener(this);
        btnBmpPosSearch.setOnClickListener(this);
        btnBmpPosClose.setOnClickListener(this);

        tvBmpPosC = (TextView) findViewById(R.id.tv_bmp_c);
        tvBmpPosC.setMovementMethod(ScrollingMovementMethod.getInstance());
        tvBmpPosS = (TextView) findViewById(R.id.tv_bmp_s);
        tvBmpPosS.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_bmp_sale:
                sendData(bmp_OP_SALE);
                break;
            case R.id.btn_bmp_void:
                sendData(bmp_OP_VOID);
                break;
            case R.id.btn_bmp_refund:
                sendData(bmp_OP_REFUND);
                break;
            case R.id.btn_bmp_close:
                //关闭控制端
                bmpSdk.shutdownClient();
                Intent intent = new Intent(this, BmpActivity.class);
                startActivity(intent);
                Logger.d("控制端断开连接");
                Toast.makeText(getApplicationContext(), "控制端断开连接", Toast.LENGTH_LONG).show();
                finish();
                break;
        }
    }

    /**
     * 组装数据
     *
     * @param opType
     */
    private void sendData(String opType) {
        if(!bmpSdk.getmSocketConnect()){
            Toast.makeText(getApplicationContext(), "蓝牙服务未连接", Toast.LENGTH_LONG).show();
            Logger.d("蓝牙服务未连接");
            socketConnect();
            return;
        }
        String resData = "";
        if (bmp_OP_SALE.equals(opType)) {
            BmpRequestSale bmpRequestSale = new BmpRequestSale();
            bmpRequestSale.setAmount("000000000011");
            bmpRequestSale.setCounterNo("Yn001");
            bmpRequestSale.setTransID("2232231231");
            bmpRequestSale.setOpID("bmpRequestSale");
            bmpRequestSale.setCashierID("4152");
            resData = GsonUtils.toJson(bmpRequestSale);
        } else if (bmp_OP_VOID.equals(opType)) {
            resData = bmp_OP_VOID;
        } else if (bmp_OP_REFUND.equals(opType)) {
            resData = bmp_OP_REFUND;
        }
        String content = (String) tvBmpPosC.getText().toString();
        content += "\n发送消息:" + resData;
        tvBmpPosC.setText(content);
        bmpSdk.sendMessage(resData);
//        runnable.run();
        running = true;
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (running) {
                    serverData = bmpSdk.getServerData();
                    if(serverData!=null && !"".equals(serverData)){
                        running = false;
                        String content = tvBmpPosS.getText().toString();
                        content += "\n服务端返回数据:" + serverData;
                        tvBmpPosS.setText(content);
                        serverData = "";
                    }
                }
            }
        };
        runnable.run();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除广播
        unregisterReceiver(bmpSdk);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
